// const hello = require('@/mysql/index') // 测试全局引入
const {ProjectType} = require('@/mysql/project_type')
const getAllProjectType = async ctx => {
    const data = await ProjectType.findAll({where: {deleted: false}})
    ctx.response.body = data
}

// module.exports = getAllProjectType
const insertProjectType = async ctx => {
    if(ctx.request.body) {
        let {name, description} = ctx.request.body
        if(!name) return ctx.response.body = global.toJson(1, `缺少分类名称`)
       const data = await  ProjectType.insertData(name,description)
        if(data) {ctx.response.body = global.toJson(0, '添加分类成功')}
    }
}
const removeProjectType = async ctx => {
    const type_id = ctx.params.id
    if(!type_id)  return ctx.response.body = global.toJson(1, `删除分类失败，未找到分类Id`)
    const data = await ProjectType.update({deleted: true},{where: {type_id}})
    if(data) ctx.response.body = global.toJson(0, '删除分类成功')

}

const updateProjectType = async ctx => {
    const type_id = ctx.params.id
    if(!type_id) return ctx.response.body = global.toJson(1, `更新分类失败，缺少分类Id`)
    const body = ctx.request.body
    const data = await ProjectType.updateData(type_id,body.name, body.description)
    console.log(data)
    if(data) ctx.body = global.toJson(0, `更新成功`)
}
export {getAllProjectType, insertProjectType, removeProjectType, updateProjectType}
